function sumOfEachLevel(root) {
  const queue = [root]
  const res = []
  while(queue.length) {
    const size = queue.length;
    let levelsum = 0
    for(let i=0;i<size;i++) {
      const node = queue.shift()
      levelsum += node.val
      node.left && queue.push(node.left)
      node.right && queue.push(node.right)
    }
    res.push(levelsum)
  }
  return res
}
import arrayToTree from '../算法/二叉树/0-数组转为二叉树.js'
const root = arrayToTree([3,9,20,null,null,15,7])
console.log(sumOfEachLevel(root)) //[3,29,22]